<?php

namespace App\Admin\Tools\ContractedCustomer;

use App\Models\AdminUsersModel;
use Encore\Admin\Grid\Tools\AbstractTool;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;

class ContractedCustomerCreateTool extends AbstractTool
{
    /**
     * @inheritDoc
     */
    public function render(): View
    {
        $users = AdminUsersModel::query()
            ->leftJoin('admin_users_related', 'admin_users.id', '=', 'admin_users_related.user_id')
            ->select('user_id', 'name');
        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) {
                $users->orWhere('admin_users_related.main_id', MAINID)
                    ->orWhere('admin_users_related.user_id', MAINID);
            } else $users->where('admin_users_related.user_id', USERID);
        }
        $users = $users->get()->toArray();

        return view('contractedCustomer.create', compact('users'));
    }
}
